<?php

define('TREEVIEW_LIB_PATH', APPLICATION_PATH .'/../lib/dbtreeview');

require_once(TREEVIEW_LIB_PATH . '/dbtreeview.php');

class myHandler implements RequestHandler{
    public function handleChildrenRequest(ChildrenRequest $req) {
        $attributes = $req->getAttributes();	
        if(!isset($attributes['MaDaiLy'])){
                die("error: attribute MaDaiLy not given");
        }
        $parentCode = $attributes['MaDaiLy'];

        $depth = 1;
        if(isset($attributes['depth'])){
                $depth = $attributes['depth'];
        }
        if($depth<1){
                die("depth error : must be > 0");
        }

        $link = mysql_connect("localhost", "tr0ngngh_sa", "th13nphuc.DB")
           or die("Unable to connect to database.");
        mysql_select_db("tr0ngngh_thienphucdb") or die("Could not select database");

        if(!mysql_query("SET CHARACTER SET utf8")){
                throw new Exception('Could not set character set UTF-8.');
        }

        $nodes = $this->getChildrenDepth($depth, 1, $parentCode);

        $response = DBTreeView::createChildrenResponse($nodes);
        return $response;


        // Close connect. 
        mysql_close($link);
    }
    
    /*     * return an array of children with subchidren... */
    private function getChildrenDepth($depth, $currentDepth, $parentCode) {

        $nodes = $this->getChildren($parentCode);

        if ($currentDepth < $depth) {
            foreach ($nodes as $node) {
                $childAttrs = $node->getAttributes();
                $childCode = $childAttrs["MaDaiLy"];
                if ($childCode == NULL) {
                    die("child MaDaiLy is null");
                }
                $children = $this->getChildrenDepth($depth, $currentDepth + 1, $childCode);
                if ($children == NULL) {
                    //die("null");
                }
                $node->setChildren($children);
                $node->setIsOpenByDefault(true);
            }
        }
        return $nodes;
    }

    /**
     * Returns the children (array)
     */
    private function getChildren($parentCode) {

        $query = sprintf("SELECT * FROM daily WHERE MaDaiLyCha='%s'", mysql_escape_string($parentCode));
        $result = mysql_query($query) or die("Query failed");
        $nodes = array();

        while ($line = mysql_fetch_assoc($result)) {
            $code = $line["MaDaiLy"];
            $text = "<b>$code</b> : " . $line["TenDaiLy"];

            $node = DBTreeView::createTreeNode($text, array("MaDaiLy" => $code));
            $node->setURL(sprintf("javascript:viewInfoDaily(%s);", $code));
            //has children
            $query2 = sprintf("SELECT * FROM daily WHERE MaDaiLyCha='%s' LIMIT 1", mysql_escape_string($code));

            $result2 = mysql_query($query2) or die("Query failed");
            if (!mysql_fetch_assoc($result2)) {
                //no children
                $node->setHasChildren(false);
                $node->setClosedIcon("../lib/dbtreeview/media/doc.gif");
            }
            $nodes[] = $node;
        }

        //ngắt kết nối.
        mysql_free_result($result);
        return $nodes;
    }

}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TraCuuTongQuatController
 *
 * @author Nghiatc
 */
class default_TracuutongquatController extends ZF_DefaultController {
    
    //put your code here
    public function indexAction() {
        $this->view->header = "Tra cứu thông tin tổng quát";
        //treeview system ThienPhuc.
        try {
            DBTreeView::processRequest(new MyHandler());
        } catch (Exception $e) {
            echo("Error:" . $e->getMessage());
        }
        
        $model_trackdonhang = new Default_Model_Trackdonhang();
        $model_daily = new Default_Model_Daily();
        
        //get data tracking đơn hàng tháng hiện tại.
        $maDLCur = 7777777;
        if($this->getRequest()->getParam('code_daily')){
            $maDLCur = $this->getRequest()->getParam('code_daily');
        } else{
            $maDLCur = 7777777;
        }
        
        //get data đại lý hiện tại.
        $objDLCur = $model_daily->getById($maDLCur);
        $tenDLCur = $objDLCur['TenDaiLy'];
        $this->view->tenDLCur = $tenDLCur;
        //get data table đơn hàng tự bán
        $tableDataTTDL = array();
        if($objDLCur){
            //tạo row data.
            $rowtable = array(  'MaDaiLy' =>$objDLCur['MaDaiLy'],
                                'TenDaiLy'=>$objDLCur['TenDaiLy'],
                                'Cap' =>$objDLCur['CapDaiLy'],
                                'NgaySinh'=>$objDLCur['NgaySinh'],
                                'SoCMND'=>$objDLCur['SoCMND'],
                                'Sdt'=>$objDLCur['Sdt'],
                                'DiaChi'=>$objDLCur['DiaChi'],
                                'NgayGiaNhap'=>$objDLCur['NgayGiaNhap']);
            // add table $tableDataDHTB
            $tableDataTTDL[] = $rowtable;
        }
        
        $this->view->tableDataTTDL = $tableDataTTDL;
        
        //get data tracking đơn hàng tháng hiện tại.
        $matrackdh = $maDLCur."_".date("m")."_".date("Y");
        $data_trackdh = $model_trackdonhang->getById($matrackdh);
        //get data table đơn hàng đại lý cấp con.
        $tableDataLN = array();
        if($data_trackdh){
            //tạo row data.
            $rowtable = array(  'LoiNhuan' =>$data_trackdh['LoiNhuan'],
                                'LoiNhuanBonus'=>$data_trackdh['Bonus']);
            // add table $tableDataLN
            $tableDataLN[] = $rowtable;
        }
        $this->view->tableDataLN = $tableDataLN;
        
    } // End indexAction()
    
    public function getinfodailyAction() {
        
        $model_trackdonhang = new Default_Model_Trackdonhang();
        $model_daily = new Default_Model_Daily();
        
        //get data tracking đơn hàng tháng hiện tại.
        $maDLCur = $this->getRequest()->getParam('code_daily');
        if($maDLCur > 0){
            //get data đại lý hiện tại.
            $objDLCur = $model_daily->getById($maDLCur);
            $tenDLCur = $objDLCur['TenDaiLy'];
            //get data table đơn hàng tự bán
            $tableDataTTDL = array();
            if($objDLCur){
                //tạo row data.
                $rowtable = array(  'MaDaiLy' =>$objDLCur['MaDaiLy'],
                                    'TenDaiLy'=>$objDLCur['TenDaiLy'],
                                    'Cap' =>$objDLCur['CapDaiLy'],
                                    'NgaySinh'=>$objDLCur['NgaySinh'],
                                    'SoCMND'=>$objDLCur['SoCMND'],
                                    'Sdt'=>$objDLCur['Sdt'],
                                    'DiaChi'=>$objDLCur['DiaChi'],
                                    'NgayGiaNhap'=>$objDLCur['NgayGiaNhap']);
                // add table $tableDataDHTB
                $tableDataTTDL[] = $rowtable;
            }

            $this->view->tableDataTTDL = $tableDataTTDL;

            //get data tracking đơn hàng tháng hiện tại.
            $matrackdh = $maDLCur."_".date("m")."_".date("Y");
            $data_trackdh = $model_trackdonhang->getById($matrackdh);
            //get data table đơn hàng đại lý cấp con.
            $tableDataLN = array();
            if($data_trackdh){
                //tạo row data.
                $rowtable = array(  'LoiNhuan' =>$data_trackdh['LoiNhuan'],
                                    'LoiNhuanBonus'=>$data_trackdh['Bonus']);
                // add table $tableDataLN
                $tableDataLN[] = $rowtable;
            }
            $this->view->tableDataLN = $tableDataLN;
            
            $jsonObject = array('ErrorCode' => '1',
                                'tenDL' => $tenDLCur,
                                'tableDataTTDL' => $tableDataTTDL,
                                'tableDataLN' => $tableDataLN
                                );
            $this->_helper->json->sendJson($jsonObject);
        } else{
            $jsonObject = array('ErrorCode' => '-1');
            $this->_helper->json->sendJson($jsonObject);
        }
        
    }
    
}

?>
